package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.pro.performance.pojo.PerformancePojo;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.dao.PerformanceDao;
import com.xbongbong.saas.domain.entity.PerformanceEntity;
import com.xbongbong.saas.model.PerformanceModel;
import com.xbongbong.sys.enums.CompanyStructTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @description: 业绩目标
 * @Author: hongxiao
 * @date: 2019-01-27 09:34
 * @since v1.0
 */
@Service("performanceModel")
public class PerformanceModelImpl implements PerformanceModel {

    @Resource
    private PerformanceDao performanceDao;

    @Override
    public Integer insert(PerformanceEntity entity){
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        return performanceDao.insert(entity);
    }
    @Override
    public Integer update(PerformanceEntity entity){
        entity.setUpdateTime((long)DateUtil.getInt());

        return performanceDao.update(entity);
    }

    @Override
    public Integer save(PerformanceEntity entity){

        if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public void insertBatch(List<PerformanceEntity> entityList) {
        performanceDao.insertBatch(entityList);
    }

    @Override
    public Integer deleteByKey( Long key,  String corpid){
        return performanceDao.deleteByKey(key , corpid);
    }

    @Override
    public PerformanceEntity getByKey( Long key,  String corpid){
        return performanceDao.getByKey(key , corpid);
    }

    @Override
    public List<PerformanceEntity> findEntitys(Map<String ,Object> param){
        return performanceDao.findEntitys(param);
    }
    @Override
    public Integer getEntitysCount(Map<String ,Object>  param){
        return performanceDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<PerformanceEntity> findEntitys(String corpid, PerformancePojo performancePojo, Set<Long> setIdIn) {
        if (setIdIn == null || setIdIn.size() == 0) {
            return new ArrayList<>();
        }
        Integer companyStructType = performancePojo.getCompanyStructType();
        Integer year = performancePojo.getYear();
        String userId = performancePojo.getUserId();
        Long departmentId = performancePojo.getDepartmentId();

        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("del", DelEnum.NORMAL.getDel());
        param.put("setIdIn", setIdIn);
        param.put("objectiveType", companyStructType);
        param.put("year", year);
        if (companyStructType == CompanyStructTypeEnum.USER.getCode()) {
            if (StringUtil.isEmpty(userId)) {
                userId = "-1";
            }
            param.put("userId", userId);
        }
        if (companyStructType == CompanyStructTypeEnum.DEPARTMENT.getCode()) {
            if (departmentId == null) {
                departmentId = -1L;
            }
            param.put("departmentId", departmentId);
        }
        List<PerformanceEntity> list = findEntitys(param);
        return list;
    }

    @Override
    public PerformanceEntity getPerformanceEntity(String corpid, PerformancePojo performancePojo) {
        Long setId = performancePojo.getSetId();
        Integer companyStructType = performancePojo.getCompanyStructType();
        Integer year = performancePojo.getYear();
        String userId = performancePojo.getUserId();
        Long departmentId = performancePojo.getDepartmentId();

        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("del", DelEnum.NORMAL.getDel());
        param.put("setId", setId);
        param.put("objectiveType", companyStructType);
        param.put("year", year);
        param.put("start", 0);
        param.put("pageNum", 1);
        //新增过滤条件
        param.put("statisticsType",performancePojo.getStatisticsType());
        if (companyStructType == CompanyStructTypeEnum.USER.getCode()) {
            if (StringUtil.isEmpty(userId)) {
                userId = "-1";
            }
            param.put("userId", userId);
        }
        if (companyStructType == CompanyStructTypeEnum.DEPARTMENT.getCode()) {
            if (departmentId == null) {
                departmentId = -1L;
            }
            param.put("departmentId", departmentId);
        }
        List<PerformanceEntity> list = findEntitys(param);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }else {
            PerformanceEntity entity = new PerformanceEntity();
            entity.setId(null);
            entity.setUserId(userId);
            entity.setDepartmentId(departmentId);
            entity.setObjectiveType(companyStructType);
            entity.setCorpid(corpid);
            entity.setSetId(setId);
            entity.setYear(year);
            return entity;
        }
    }
}
